﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using RSALicUI;

namespace RSALicManager
{
    public partial class MakeQrLicForm : Form
    {
        public MakeQrLicForm()
        {
            InitializeComponent();

            IMessage = "输入客户的注册码，以生成授权码";
            OMessage = "生成的授权码，提供给客户进行软件授权";
        }

        public string OTitle
        {
            get => qrMaker.Title;
            set => qrMaker.Title = value;
        }

        public string OMessage
        {
            get => lOutput.Text;
            set => lOutput.Text = value;
        }


        public string ITitle
        {
            get => qrParser.Title;
            set => qrParser.Title = value;
        }

        public string IMessage
        {
            get => lInput.Text;
            set => lInput.Text = value;
        }

        public string Link
        {
            get => lLink.Text;
            set => lLink.Text = value;
        }

        public Image GetOutputQr(Func<string, string> trans)
        {
            var data = qrParser.GetQrData();
            qrMaker.SetData(trans.Invoke(data));
            return qrMaker.GetQrImage();
        }

        public void SetInputQr(Image qrImg)
        {
            qrParser.SetData(qrImg);
        }

        string priReg;

        private void QrParser_QrDetected(string reg)
        {
            if (string.Compare(reg, priReg) == 0)
            {
                return;
            }

            if (string.Compare(reg, CommResources.TestQrData) == 0)
            {
                return;
            }

            priReg = reg;
            var res = MakeLic(reg);
            if (!string.IsNullOrEmpty(res))
            {
                qrMaker.SetData(res);
            }
            else
            {
                lInput.Text = CommResources.QrTips;
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (!string.IsNullOrEmpty(CommResources.Website))
            {
                lLink.Text = CommResources.Website;
            }
            else
            {
                lLink.Visible = false;
            }

            qrParser.QrDetected += QrParser_QrDetected;
        }

        string priKey;
        string MakeLic(string reg)
        {

            if (string.IsNullOrEmpty(priKey))
            {
                using (var dlg = new OpenFileDialog())
                {
                    dlg.Title = "加载私钥";
                    dlg.Filter = "xml文件|*.xml";
                    var res = dlg.ShowDialog();
                    if (res == DialogResult.OK)
                    {
                        var file = dlg.FileName;

                        using (var reader = new StreamReader(file, Encoding.UTF8))
                        {
                            priKey = reader.ReadToEnd();
                            reader.Close();
                        }
                    }
                }
            }

            if (string.IsNullOrEmpty(priKey))
            {
                return null;
            }

            var primaker = new RSALicCore.PriLicManager(priKey);
            return primaker.MakeLic(reg);
        }

        private void lLink_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start(lLink.Text);
        }

        private void menuAdvanceTool_Click(object sender, EventArgs e)
        {
            var atf = new AuthToolsForm();
            atf.ShowDialog();
        }
    }
}
